<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['admin_is_login']){
			$this->error('还未登录，请登录',U('admin/auth/do_login'));
		}
	}
    public function index(){
    	$product_model = M('product');
    	$count = $product_model->count();
    	$Page = new \Org\Util\Page($count,10);
    	$products = $product_model->field('*,product.id as id')->join('product_catalog ON product.cata_id = product_catalog.id','left')->order('product.id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$this->assign('products',$products);
    	$this->assign('page',$Page->show());
    	$this->display();
    }
    public function add(){
        $this->assign('level1',M('product_catalog')->where('parent_id = 0')->select());
    	$this->display();
   }
   public function getCatalog(){
    $id = I('get.id');
    $data = M('product_catalog')->where('parent_id ='.$id)->select();
    $this->ajaxReturn($data);
   }
    public function do_add(){
    	$rules = array(     
            array('title','require','标题不能为空！'),
            array('cata_id','require','请选择分类！'),
            array('img','require','请选择图片！'),
            array('price','require','商品价格不能为空！'),
            array('stock','require','请填写库存！'),
            array('desc','require','请填写商品描述！'),
            array('content','require','请填写商品详情！')
        );
        $product = M('product');
        if (!$product->validate($rules)->create()){    
             $this->error($product->getError());
        }else{ 
        	M('product')->add(array(
        		'title' => I('post.title'),
        		'cata_id' => I('post.cata_id'),
        		'img' => I('post.img'),
        		'price' => I('post.price'),
        		'stock' => I('post.stock'),
        		'desc' => I('post.desc'),
        		'content' => I('post.content')
        	));
        	$this->success('商品保存成功',U('admin/product/index'));
        }
    }
    public function edit($id){
        $product = M('product')->where('id ='.$id)->find();
        $level3 = $product['cata_id'];
        $level3_catalog = M('product_catalog')->where('id ='.$level3)->find();
        $level2 = $level3_catalog['parent_id'];
        $level2_catalog = M('product_catalog')->where('id ='.$level2)->find();
        $level1 = $level2_catalog['parent_id'];
        $this->assign('level3',$level3);
        $this->assign('level2',$level2);
        $this->assign('level1',$level1);
        $this->assign('product',$product);
        $this->assign('level1_data',M('product_catalog')->where('parent_id = 0')->select());
        $this->display();
    }
    public function do_edit(){
        $id = I('post.id');
        M('product')->where('id = '.$id)->save(array(
            'title' => I('post.title'),
            'cata_id' => I('post.cata_id'),
            'img' => I('post.img'),
            'price' => I('post.price'),
            'stock' => I('post.stock'),
            'desc' => I('post.desc'),
            'content' => I('post.content')
        ));
        $this->success('编辑商品成功',U('admin/product/index'));
    } 
    public function delete($id){
        M('product')->where('id ='.$id)->delete($id);
        $this->success('删除商品成功',U('admin/product/index'));
    }

    public function disable($id){
        M('product')->where('id ='.$id)->save(array('status' => '0'));
        $this->success('下架成功',U('admin/product/index'));
    }
    public function enable($id){
        M('product')->where('id ='.$id)->save(array('status' => '1'));
        $this->success('上架成功',U('admin/product/index'));
    }
}